﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using NUnit.Framework;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;

using CloudSoft.Extensions;

namespace ERPStore.Payment.Paypal.Tests.Controllers
{
	[TestFixture]
	public class PaypalExpressControllerTests : ERPStore.Tests.TestBase
	{
		[SetUp]
		public override void Initialize()
		{
			base.Initialize();
		}

		[Test]
		public void AuthenticatedConfirmation()
		{
			var paypalService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Payment.Paypal.Services.PaypalExpressService>();
			var settings = GlobalConfiguration.Configuration.DependencyResolver.GetService<Models.Settings>();

			var cartController = CreateAuthenticatedController<ERPStore.Web.Cart.Controllers.CartController>();
			var checkoutController = CreateAuthenticatedController<ERPStore.Checkout.Controllers.CheckoutController>();

			cartController.AddItemWithQuantity("XBOX360", 10, null);
			var cartResult = cartController.Index() as System.Web.Mvc.ViewResult;
			var cart = cartResult.ViewData.Model as ERPStore.Models.OrderCart;

			var shippingResult = checkoutController.Shipping() as System.Web.Mvc.ViewResult;

			Assert.AreEqual(shippingResult.ViewName, "connectedshipping");
			Assert.IsNotNull(shippingResult.ViewData["SelectedAddressId"]);

			var connectedShippingResult =  checkoutController.ConnectedShipping(0, null) as System.Web.Mvc.RedirectToRouteResult;

			Assert.AreEqual(connectedShippingResult.RouteName, global::ERPStore.Web.Routing.ERPStoreRoutes.CHECKOUT_CONFIGURATION);

			var paypalController = CreateAuthenticatedController<ERPStore.Payment.Paypal.Controllers.PaypalExpressController>();

			paypalController.HttpContext.Items["language"] = "fr";
			var result = paypalController.Confirmation("on") as System.Web.Mvc.RedirectResult;

			Assert.IsTrue(result.Url.StartsWith(settings.PaypalCheckoutUrl));

			var token = new Uri(result.Url).GetParameters()["token"];
			var transactionDetail = new global::ERPStore.Payment.Paypal.com.paypalobjects.GetExpressCheckoutDetailsResponseDetailsType();
			transactionDetail.Token = token;
			var backFromPaypalResult = paypalController.BackFromPaypalWithDetails(cart.Code, token, "payerId", transactionDetail);

			Assert.AreNotEqual(backFromPaypalResult, "emptycart");
			Assert.AreNotEqual(backFromPaypalResult, "invalidcart");

			var finalizedResult = paypalController.Finalized(backFromPaypalResult) as System.Web.Mvc.ViewResult;
			Assert.IsNotNull(finalizedResult);
			var finalizedViewName = string.Format("~/views/checkout/{0}", paypalService.FinalizedViewName);
			Assert.AreEqual(finalizedResult.ViewName, finalizedViewName);

			var order = finalizedResult.Model as global::ERPStore.Models.Order;

		}

		public void Finalize()
		{

		}

	}
}
